package com.cskaoyan.gateway.controller.user;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IKaptchaService;
import com.mall.user.IUserLoginService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author hanshuai
 * @version 1.0
 * @description 用户登录网关
 * @date 2021/12/12 13:45
 */
@RestController
@RequestMapping("user")
public class UserLoginController {

    @Reference(timeout = 3000,retries = 2, check = false)
    IUserLoginService userLoginService;

    @Reference(timeout = 3000,retries = 2, check = false)
    IKaptchaService kaptchaService;

    @PostMapping("login")
    @Anoymous
    public ResponseData userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request, HttpServletResponse httpServletResponse) {
        //判空
        try{
            userLoginRequest.requestCheck();
        }catch (Exception e){
            return new ResponseUtil().setErrorMsg(SysRetCodeConstants.REQUEST_DATA_NOT_EXIST.getMessage());
        }
        String userName = userLoginRequest.getUserName();
        String password = userLoginRequest.getUserPwd();
        String captcha = userLoginRequest.getCaptcha();
        //判断验证码是否匹配
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(uuid);
        kaptchaCodeRequest.setCode(captcha);
        //验证
        KaptchaCodeResponse kaptchaCodeResponse = kaptchaService.validateKaptchaCode(kaptchaCodeRequest);
//        String imageCode = kaptchaCodeResponse.getImageCode();
        //验证码不正确就给出提示
        if (!kaptchaCodeResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            return new ResponseUtil().setErrorMsg(kaptchaCodeResponse.getMsg());
        }
        //在这里设置 防止 没有 序列化
        UserLoginResponse response = userLoginService.userLogin(userName, password);
        // 一天的有效期
        javax.servlet.http.Cookie cookie = CookieUtil.genCookie("access_token", response.getToken(), "/", 60 * 60 * 24);
        Cookie cookie1 = CookieUtil.genCookie("SameSite", "None", null, -1);
        CookieUtil.setCookie(httpServletResponse, cookie);
        CookieUtil.setCookie(httpServletResponse, cookie1);
        if (!response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setErrorMsg(response.getMsg());
        }
        return new ResponseUtil().setData(response);
    }

    @GetMapping("/login")
    public ResponseData userLoginGet(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        String token = null;
        for (Cookie cookie : cookies) {
            if ("access_token".equals(cookie.getName())) {
                token = cookie.getValue();
            }
        }
        //把token传给Service处理
        UserLoginGetResponse response = userLoginService.userLoginGet(token);
        if (!SysRetCodeConstants.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setErrorMsg(response.getMsg());
        }
        return new ResponseUtil().setData(response);
    }
}
